<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_edu_live_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),
			
			array('op'=>'edit','title'=>'添加直播'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'封面','type'=>'image'),								
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		),
		'search'=>array(
			array('name'=>'title','title'=>'名称','width'=>15,'type'=>'string'),
			array('name'=>'status','title'=>'状态','width'=>15,'type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_edu_live_list',array('id'=>$id));
	$item['menu']=json_decode($item['menu'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_edu_live_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'post'=>'post',
		'desc'=>'直播设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),
			
			array('op'=>'edit','title'=>'添加直播')
		),
		'edits'=>array(
		array(
		'title'=>'直播设置',
		'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'封面','type'=>'image'),
			array('name'=>'time','title'=>'直播时间','type'=>'time'),
			array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'group'),
			array('name'=>'type','title'=>'直播平台类型','type'=>'radio','options'=>'type'),
			array('name'=>'pt','title'=>'直播平台','type'=>'radio','options'=>'sxt','show'=>array('type'=>2)),
			array('name'=>'pt','title'=>'直播平台','type'=>'radio','options'=>'dsf','show'=>array('type'=>1)),		
			array('name'=>'url','title'=>'url地址','type'=>'string','help'=>'填写完整直播地址'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'ishome','title'=>'是否推荐','type'=>'radio','options'=>'is'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'radio','options'=>'is'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			),
			),
	
		array(
		'title'=>'分享/公告设置',
		'list'=>array(
			array('name'=>'notice','title'=>'公告内容','type'=>'string'),
			array('name'=>'share_title','title'=>'分享标题','type'=>'string'),
			
			array('name'=>'share_image','title'=>'分享图片','type'=>'string'),
			array('name'=>'share_desc','title'=>'分享描述','type'=>'string'),
			),
		),
	),
	);
	$return['type']=array('1'=>'第三方平台','3'=>'企业微信直播');
	//$return['type']=array('1'=>'第三方平台','2'=>'智能摄像头');
	$return['dsf']=array('3'=>'斗鱼直播','5'=>'花椒直播','6'=>'映客直播');	
	$return['dsf']=array('6'=>'映客直播');		
	//$return['sxt']=array('1'=>'青果直播','2'=>'萤石直播');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['time']){
		exi('请填写时间','error');
	}
	if(!$postdata['url']&& !$postdata['shipin']){
		exi('请填写URL','error');
	}
	$data=array_fields(array('image','title','type','time','pt','notice','status','share_image','share_desc','share_title','menu','group_id','url','ishome','ishot','paixu','shipin'),$postdata);
	$data['menu']=json_encode($data['menu']);
	$id=intval($postdata['id']);
	if($id){
				pdo_update('jy_edu_live_list',$data,array('id'=>$id));
				exi('设置成功！','','list');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$item=$this->plugin_setting('setting');
				$identifier= $item['admin'];
				$sign=pdo_getcolumn('jy_cmslive_sign',array('uniacid'=>$_SESSION['uniacid'],'name'=>$item['admin']),'sign');				
				if(strlen($sign)<10){
						pdo_delete('jy_cmslive_sign',array('uid'=>$_SESSION['uid']));
						$adminkey=$item['adminkey'];
						if(!$identifier || !$item['appid'] || !$adminkey){
							exi('云通讯设置:APPID,accountType,管理员账号,private,public不能为空！','error');
						}
						$sign=$this->getusersig($identifier, $item['appid'], $adminkey);
						if(strlen($sign)>10){
							pdo_insert('jy_cmslive_sign',array('uniacid'=>$_SESSION['uniacid'],'name'=>$identifier,'createtime'=>TIMESTAMP,'sign'=>$sign));
						}else{
							exi('生成用户签名失败，请检查云服务设置是否正确','error');
						}
						
				}
				$url='https://console.tim.qq.com/v4/group_open_http_svc/create_group?usersig='.$sign.'&identifier='.$identifier.'&sdkappid='.$item['appid'].'&random='.random(32,true).'&contenttype=json';
				$name=$data['title'].random(4);
				$post=array('Type'=>'AVChatRoom','Name'=>$name);
				$re=cfc('http')->ihttp_post($url,json_encode($post));
				$re['content']=json_decode($re['content'],true);
				if($re['content']['ActionStatus']=='OK'){
						$data['chatroomid']=$re['content']['GroupId'];
				}else{
					pdo_update('jy_cmslive_sign',array('sign'=>''),array('uniacid'=>$_SESSION['uniacid'],'name'=>$item['admin']));
					exi('创建群聊失败，请检查云服务设置是否正确'.$re['content']['ErrorCode'],'error','/setting/');
				}
				pdo_insert('jy_edu_live_list',$data);
				exi('添加成功！','','list');
		}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_edu_live_list',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_edu_live_list',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>